using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Blog.IRepository;
using Blog.Model;
using SqlSugar;

namespace Blog.Repository
{
    public class BlogNewsRepository:BaseRepository<BlogNews>, IBlogNewsRepository
    {
        public override async Task<List<BlogNews>> QueryAsync()
        {
            return await Context.Queryable<BlogNews>()
                .Mapper(c => c.TypeInfos, c => c.TypeId, c => c.TypeInfos.Id)
                .Mapper(c => c.WriterInfos, c => c.WriterId, c => c.WriterInfos.Id)
                .ToListAsync();
        }

        public override async Task<List<BlogNews>> QueryAsync(Expression<Func<BlogNews, bool>>func)
        {
            return await Context.Queryable<BlogNews>()
                .Where(func)
                .Mapper(c => c.TypeInfos, c => c.TypeId, c => c.TypeInfos.Id)
                .Mapper(c => c.WriterInfos, c => c.WriterId, c=> c.WriterInfos.Id)
                .ToListAsync();
        }

        public override async Task<List<BlogNews>> QueryAsync(int page, int size, RefAsync<int>total)
        {
            return await Context.Queryable<BlogNews>()
                .Mapper(c => c.TypeInfos, c => c.TypeId, c => c.TypeInfos.Id)
                .Mapper(c => c.WriterInfos, c => c.WriterId, c => c.WriterInfos.Id)
                .ToPageListAsync(page, size, total);
        }

        public override async Task<List<BlogNews>> QueryAsync(Expression<Func<BlogNews, bool>>func, int page, int size, RefAsync<int> total)
        {
            return await Context.Queryable<BlogNews>()
                .Where(func)
                .Mapper(c => c.TypeInfos, c => c.TypeId, c => c.TypeInfos.Id)
                .Mapper(c => c.WriterInfos, c => c.WriterId, c => c.WriterInfos.Id)
                .ToPageListAsync(page, size, total);
        }
    }
}